package com.youcheng.controller;

import com.youcheng.utils.Constants;
import com.youcheng.utils.Helper;
import io.swagger.annotations.Api;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

@Api(tags = {"rabbitmq相关接口文档"})
@Controller
public class RabbitController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @GetMapping("/r/product")
    @ResponseBody
    public void product(){
        for (int i = 0; i < 50; i++) {
            String context = "hello 【 "+i+" 】==========" + Helper.getCurrentDate(null);
            amqpTemplate.convertAndSend(Constants.QUEUE_NAME,context);
        }

    }

    @GetMapping("/r/publish")
    @ResponseBody
    public void product1(){
        String context = "hello ==========" + Helper.getCurrentDate(null);
        System.out.println(context);
        //交换机名称 fanoutExchange
        amqpTemplate.convertAndSend("fanoutExchange","",context);
    }

    @GetMapping("/r/routing")
    @ResponseBody
    public void product2(){
        String context = "hello ==========" + Helper.getCurrentDate(null);
        System.out.println(context);
        //交换机名称 fanoutExchange
        ArrayList<String> list = new ArrayList<>();
        list.add("direct.a.key");
        list.add("direct.b.key");

        amqpTemplate.convertAndSend("youExchange","direct.a.key",list);
        amqpTemplate.convertAndSend("youExchange","direct.b.key",context);
    }

}
